<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
	<meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" media="screen" href="/css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" media="screen" href="/css/dataTables.bootstrap.min.css">
	<link rel="stylesheet" type="text/css" media="screen" href="/layui/css/layui.css">
	<link rel="stylesheet" href="/css/treetable/jquery.treetable.css" />
	<link rel="stylesheet" href="/css/treetable/jquery.treetable.theme.default.css" />
	<script src="/js/libs/jquery-2.1.1.min.js"></script>
	<link rel="stylesheet" type="text/css" href="/css/bootstrap-select.min.css">
	<script src="/js/bootstrap/bootstrap.min.js"></script>
	<script src="/js/bootstrap/bootstrap-select.min.js"></script>
	<script src="/js/jq.js"></script>
	<script src="/js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
	<script src="/js/common.js"></script>
	<script src="/layui/layui.js"></script>
	<script src="/js/dict.js"></script>
	<script src="/js/libs/jquery.ztree.all-3.5.min.js"></script>
	<script src="/js/my/ztree-menu.js"></script>
	<script src="/js/my/permission.js"></script>
	<script src="/js/plugin/datatables/jquery.dataTables.min.js"></script>
	<script src="/js/plugin/datatables/dataTables.bootstrap.min.js"></script>
	<script src="/js/libs/jquery.treetable.js"></script>
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 col-md-offset-2 col-lg-offset-2">
	<form class="form-horizontal" onsubmit="return false" id="form">
		<fieldset>
			<div class='form-group'>
				<label class='col-md-4 control-label'>上级部门</label>
				<div class='col-md-8'>
					<input class='form-control' type='hidden' name='id' id='id'>
					<input class='form-control' type='hidden' name='parentId' id='parentId'>
					<input class='form-control' type='hidden' name='parentIds' id='parentIds'>
					<input style="width: 90%; float: left" class='form-control' type='text'
						   name='parentName' id='parentName' placeholder='上级部门'
						   data-bv-notempty='true' data-bv-notempty-message='上级部门 不能为空' readonly >
					<button style="width: 10%;float: right; height: 32px; line-height: 32px;" class="layui-btn layui-btn-sm"
							onclick="showSpecialTree('parentId', 'parentName', 'dept', '部门', '总公司', 'select', 'fasDeptTree')">
						<i class="layui-icon">&#xe615;</i>
					</button>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>所属公司</label>
				<div class='col-md-8'>
					<select class="form-control input-sm selectpicker show-tick" name="company" id="company"
							title="所属公司" data-live-search="false" data-bv-notempty='true'
							data-bv-notempty-message='所属公司 不能为空'></select>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>机构名称</label>
				<div class='col-md-8'>
					<input class='form-control' placeholder='机构名称' type='text' name='name' id='name'
						   data-bv-notempty='true' data-bv-notempty-message='机构名称 不能为空'>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>机构类型</label>
				<div class='col-md-8'>
					<select class="form-control input-sm selectpicker show-tick" name="type" id="type"
							data-live-search="true"></select>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>机构等级</label>
				<div class='col-md-8'>
					<select class="form-control input-sm selectpicker show-tick" name="level" id="level"
							data-live-search="true"></select>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>是否公共模块使用</label>
				<div class='col-md-8'>
					<select class="form-control input-sm selectpicker show-tick" name="isPublic" id="isPublic"
							data-live-search="true" disabled="disabled"></select>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>是否资产模块使用</label>
				<div class='col-md-8'>
					<select class="form-control input-sm selectpicker show-tick" name="isFas" id="isFas"
							data-live-search="true" disabled="disabled"></select>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>主负责人</label>
				<div class='col-md-8'>
					<input class='form-control' type='hidden' name='primaryUserId' id='primaryUserId'>
					<input style="width: 90%;float: left;" class='form-control' placeholder='主负责人' type='text'
						   name='primaryUserName' id='primaryUserName' readonly>
					<button style="width: 10%;float: right; height: 32px; line-height: 32px;" class="layui-btn layui-btn-sm"
							onclick="showTableAndFlushFields('user', 'user', '用户', true, false, 'primaryUserId:id', 'primaryUserName:nickname')">
						<i class="layui-icon">&#xe615;</i>
					</button>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>副负责人ID</label>
				<div class='col-md-8'>
					<input class='form-control' type='hidden' name='deputyUserId' id='deputyUserId' >
					<input style="width: 90%;float: left;" class='form-control' placeholder='副负责人' type='text'
						   name='deputyUserName' id='deputyUserName' readonly>
					<button style="width: 10%;float: right; height: 32px; line-height: 32px;" class="layui-btn layui-btn-sm"
							onclick="showTableAndFlushFields('user', 'user', '用户', true, false, 'deputyUserId:id', 'deputyUserName:nickname')">
						<i class="layui-icon">&#xe615;</i>
					</button>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>排序</label>
				<div class='col-md-8'>
					<input class='form-control' placeholder='排序' type='text' name='sort' id='sort'
						   data-bv-notempty='true' data-bv-notempty-message='排序 不能为空'>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>备注</label>
				<div class='col-md-8'>
					<input class='form-control' placeholder='备注' type='text' name='remark' id='remark'>
				</div>
			</div>
			<div class="form-actions">
				<div class="row" align="center">
					<div class="col-md-12">
						<!--<button class="btn btn-primary" onclick="location.href='deptList.html'">返回</button>-->
						<button class="btn btn-primary" type="submit" onclick="save()">
							<i class="fa fa-save"></i> 保存
						</button>
					</div>
				</div>
			</div>

		</fieldset>
	</form>
</div>

	<script type="text/javascript">
		layui.use(['layer','laydate'], function(){
		    var layer = layui.layer;
		});

        showDictSelect("type", "dept_type");
        showDictSelect("level", "dept_level");
		showDictSelect("isPublic", "boolean");
		showDictSelect("isFas", "boolean");
		showDictSelect('company', 'company', '', 'beep');

        var parentId;
        var parentName;
        var isSelected = false; // 树结构是否选择

        var primaryUserId = '';
        var primaryUserName = '';

		initData();
		
		function initData(){
			var id = getUrlParam("id");
			if(id){
				$.ajax({
					type : 'get',
					url : '/dept/'+id,
					async : false,
					success : function(json) {
                        if (json.code === 0) {
                        	debugger;
                            var data = json.data;
                            $('#id').val(data.id);
                            $('#parentId').val(data.parentId);
                            $('#parentName').val(data.parentName);
                            $('#parentIds').val(data.parentIds);
                            $('#name').val(data.name);
                            $('#company').val(data.company);
                            // $('#type').val(data.type);
                            $('#type').selectpicker('val', data.type);
                            $('#level').val(data.level);
                            $('#primaryUserId').val(data.primaryUserId);
                            $('#primaryUserName').val(data.primaryUserName);
                            $('#deputyUserId').val(data.deputyUserId);
                            $('#deputyUserName').val(data.deputyUserName);
                            $('#sort').val(data.sort);
                            $('#remark').val(data.remark);
                            $('#isPublic').val(data.isPublic);
                            $('#isFas').val(data.isFas);
                        }

					}
				});
			}
		}

		function save() {
			$('#form').bootstrapValidator();
			var bootstrapValidator = $("#form").data('bootstrapValidator');
			bootstrapValidator.validate();
		    if(!bootstrapValidator.isValid()){
			   return;
		    }
		    
		    var formdata = $("#form").serializeObject();

			$.ajax({
				type : 'put',
				url : '/dept',
				contentType: "application/json; charset=utf-8",  
				data : JSON.stringify(formdata),
				success : function(json) {
                    if (json.code === 0) {
                        layer.msg("修改成功", {shift: -1, time: 1000}, function () {
                            // location.href = "deptList.html";
                            parent.location.reload();//关闭iFrame
                        });
                    } else {
                        layer.msg("修改失败：" + json.message, {shift: -1, time: 1000});
                    }
				}
			});
		}
		
	</script>
</body>
</html>
